package com.huatao.oss.service.impl;

import com.huatao.oss.entity.FileInfo;
import com.huatao.oss.service.StorageService;
import com.huatao.oss.util.MinioUtils;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

@Service("minioStorageServiceImpl")
public class MinioStorageServiceImpl implements StorageService {

    @Resource
    private MinioUtils minioUtils;

    @Override
    @SneakyThrows
    public void createBucket(String bucket) {
        minioUtils.createBucket(bucket);
    }

    @Override
    @SneakyThrows
    public void updateFile(MultipartFile file, String bucket, String objectName) {
        minioUtils.createBucket(bucket);
        if (objectName != null) {
            minioUtils.updateFile(file.getInputStream(), bucket, objectName + "/" + file.getName());
        } else {
            minioUtils.updateFile(file.getInputStream(), bucket, file.getName());
        }
    }

    @Override
    @SneakyThrows
    public List<String> getAllBucket() {
        return minioUtils.getAllBucket();
    }

    @Override
    @SneakyThrows
    public List<FileInfo> getAllFile(String bucket) {
        return minioUtils.getAllFile(bucket);
    }

    @Override
    @SneakyThrows
    public InputStream downLoad(String bucket, String objectName) {
        return minioUtils.downLoad(bucket, objectName);
    }

    @Override
    @SneakyThrows
    public void deleteBucket(String bucket) {
        minioUtils.deleteBucket(bucket);
    }

    @Override
    @SneakyThrows
    public void deleteFile(String bucket, String objectName) {
        minioUtils.deleteFile(bucket, objectName);
    }
}
